package online.hxstar.cloud.provider.stream.controller;

import online.hxstar.cloud.provider.stream.bind.HelloBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xinghu
 * @version 1.0
 * @date 2019/11/12 11:27 上午
 */
@RestController
public class ProducerController {

	private MessageChannel greet;

	public ProducerController(HelloBinding binding) {
		greet = binding.greetingOutPut();
	}

	@GetMapping("/greet/{name}")
	public void publish(@PathVariable String name, @RequestParam Integer size) {
		for (int i = 0; i < size; i++) {
			String greeting = "Hello, " + name + i + "!";
			Message<String> msg = MessageBuilder.withPayload(greeting)
					.build();
			this.greet.send(msg);
		}
	}
}
